package in.muton.edushare.pages.cp.semCP;

import in.muton.edushare.components.messaging.api.UserMessage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.AcademicYearModelDAO;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class AllotElectivePanel extends EdusharePanel implements IHeaderContributor {

	private static final long serialVersionUID = -3164485425072311061L;
	private IModel<Universities> univModel;
	private Log log = LogFactory.getLog(AllotElectivePanel.class);
	private Form<Void> f;
	private WebMarkupContainer warnUser;
	
	public AllotElectivePanel(String id, IModel<Universities> model, EduShareBasePage page) {
		super(id, model, page);
		this.univModel = model;
		
		//show allot elective button
		WebMarkupContainer wmc = new WebMarkupContainer("allotElectiveForm"){
			
			private static final long serialVersionUID = -1092949787835980399L;

			@Override
			public boolean isVisible() {
				return univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_ALLOW_ELECTIVE_SELECTION;
			}
		};
		add(wmc);
		f = new Form<Void>("aef"){
			
			private static final long serialVersionUID = 6088705997702029562L;

			@Override
			public void onSubmit() {
				IModel<Universities> currModel = univModel;
				String semName = new AcademicYearModelDAO(
						getORMInterface(), currModel.getObject())
						.getActiveSemester().getSemesterName();
				String message = "Elective choosing time for semester "
						+ semName
						+ ", "
						+ currModel.getObject().getCurrentAcadYear()
						+ " has been closed."
						+ " Students will be assigned to their electives at midnight.";
				CayenneList<Users> userList = new UnivDataHandler(currModel)
						.getEveryOne();

				log.debug("About to send message \n" + message);
				new UserMessage(message).setReciever(userList).setReciever(
						currModel.getObject()).setPrivacy(
						Messages.PRIVACY_TYPE_PUBLIC).send();
				univModel.getObject()
						.setElectiveSelectionTime(
								Universities.UNIV_LOCK_ELECTIVE_SELECTION);
				getORMInterface().commitAllChanges();
			}
		};
		wmc.add(f);
		AcademicYearModelDAO acadMDAO = new AcademicYearModelDAO(getORMInterface(), univModel.getObject());
		String currSem = acadMDAO.getSemesterName(acadMDAO.getActiveSemester().getSequenceNo(), univModel.getObject().getCurrentAcadYear());
		f.add(new Label("currSem",currSem));
		f.add(new Label("currSemBut",currSem));
		f.add(warnUser=new WebMarkupContainer("warnUser"));
		warnUser.setOutputMarkupId(true);
		
		//show go to semester change page
		WebMarkupContainer wmc1 = new WebMarkupContainer("goToChangeSemester"){

			private static final long serialVersionUID = 1006329591741494644L;

			@Override
			public boolean isVisible() {
				return univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_CLOSE_ELECTIVE_SELECTION;
			}
		};
		add(wmc1);
		wmc1.add(new Label("acsem",
				new AcademicYearModelDAO(getORMInterface(), univModel.getObject())
						.getActiveSemester()
						.getSemesterName()
						+	", " 
						+	univModel
							.getObject()
							.getCurrentAcadYear())
						);
		wmc1.add(new Link<Void>("chsemlnk"){

			private static final long serialVersionUID = 6903366144664328319L;

			@Override
			public void onClick() {
				((SemesterCP)getBasePage()).jumpTo(0);
			}
			
		});
		
		//If semester change is pending
		WebMarkupContainer wmcSCP = new WebMarkupContainer("semChangePending"){
			
			private static final long serialVersionUID = 5624549840692854889L;

			@Override
			public boolean isVisible() {
				return univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_SCHEDULE_SEMESTER_CHANGE;
			}
		};
		add(wmcSCP);
		
		//If allot elective is pending
		WebMarkupContainer wmcEAP = new WebMarkupContainer("eleAllotPending"){
			
			private static final long serialVersionUID = -8093711605405613412L;

			@Override
			public boolean isVisible() {
				return univModel.getObject().getElectiveSelectionTime() == Universities.UNIV_LOCK_ELECTIVE_SELECTION;
			}
		};
		add(wmcEAP);
		
		add(new Label("es1", ApplicationConstants.APPLICATION_NAME));
	}
	
	public void renderHead(IHeaderResponse resp) {
		resp.renderOnDomReadyJavascript("" +
				"$('#"+warnUser.getMarkupId()+"').dialog({"+
					    "autoOpen: false,"+
					    "width: 400,"+
					    "modal: true,"+
					    "buttons: {"+
					        "\"Ok\": function() {"+ 
					        "$(this).dialog(\"close\");"+
					        "$('#"+f.getMarkupId()+"').submit();"+
					         "return true; "+
					        "}, "+
					        "\"Cancel\": function() {"+ 
						    "$(this).dialog(\"close\");"+ 
					            "return false; "+
					        "} "+
					    "}"+
					"});"+
				"$('#eallotSubm').click(function(){$('#"+warnUser.getMarkupId()+"').dialog('open');return false;});");
	}

}
